package com.zsyh.jiujiu.weight;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.ClassOneBean;
import com.zsyh.jiujiu.net.MyFragementObserver;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import razerdp.basepopup.BasePopupWindow;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class PopClassType extends BasePopupWindow {

    private OnSureClickListener onSureClickListener;
    private BaseQuickAdapter<ClassOneBean.DataBean, BaseViewHolder> mAdapter;
    List<ClassOneBean.DataBean> list=new ArrayList<>();
    private FragmentActivity mContext;
    public PopClassType(Context context, FragmentActivity mContext){
        super(context);
        this.mContext=mContext;
        RecyclerView rvlist1=findViewById(R.id.rvlist1);
        mAdapter=new BaseQuickAdapter<ClassOneBean.DataBean, BaseViewHolder>(R.layout.item_popone,list) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, ClassOneBean.DataBean item) {
                helper.setText(R.id.name,item.getClassName());
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onSureClickListener.callback(item.getId(),item.getClassName());
                        dismiss();
                    }
                });
            }
        };
        rvlist1.setLayoutManager(new LinearLayoutManager(context));
        rvlist1.setAdapter(mAdapter);
        Classfenlei("");

    }



    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.pop_classtype);
    }

    public void setOnSureClickListener(OnSureClickListener onSureClickListener) {
        this.onSureClickListener = onSureClickListener;
    }

    public interface OnSureClickListener {

        void callback(String typeid,String name);

    }


    /**
     * 分类
     */
    public void Classfenlei(String pid) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getGoodsClass(pid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(mContext, false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("tixian", JsonStr + "");
                        Gson gson = new Gson();
                        ClassOneBean sucessBean = gson.fromJson(JsonStr, ClassOneBean.class);
                        ClassOneBean.DataBean all=new ClassOneBean.DataBean();
                        all.setId("");
                        all.setClassName("全部");
                        list.add(all);
                        for (int i = 0; i < sucessBean.getData().size(); i++) {
                            list.add(sucessBean.getData().get(i));
                        }

                        mAdapter.notifyDataSetChanged();

                    }
                });
    }
}
